﻿Public Class TMyTableColumns
    Private _List As New List(Of TMyTableColumn)
    Private _Owner As IHasColumns
    Friend Sub New(ByVal AOwner As IHasColumns)
        _Owner = AOwner
    End Sub
    Private ReadOnly Property Query() As TMyQuery
        Get
            Return _Owner.Query
        End Get
    End Property
    Public Sub Refresh()
        _List.Clear()
        Query.Clear()
        Query.SQL.AppendLine("select")
        Query.SQL.AppendLine(" TABLE_CATALOG")
        Query.SQL.AppendLine(",TABLE_SCHEMA")
        Query.SQL.AppendLine(",TABLE_NAME")
        Query.SQL.AppendLine(",COLUMN_NAME")
        Query.SQL.AppendLine(",ORDINAL_POSITION")
        Query.SQL.AppendLine(",COLUMN_DEFAULT")
        Query.SQL.AppendLine(",IS_NULLABLE")
        Query.SQL.AppendLine(",DATA_TYPE")
        Query.SQL.AppendLine(",CHARACTER_MAXIMUM_LENGTH")
        Query.SQL.AppendLine(",CHARACTER_OCTET_LENGTH")
        Query.SQL.AppendLine(",NUMERIC_PRECISION")
        Query.SQL.AppendLine(",NUMERIC_SCALE")
        Query.SQL.AppendLine(",CHARACTER_SET_NAME")
        Query.SQL.AppendLine(",COLLATION_NAME")
        Query.SQL.AppendLine(",COLUMN_TYPE")
        Query.SQL.AppendLine(",COLUMN_KEY")
        Query.SQL.AppendLine(",EXTRA")
        Query.SQL.AppendLine(",PRIVILEGES")
        Query.SQL.AppendLine(",COLUMN_COMMENT")
        Query.SQL.AppendLine("from")
        Query.SQL.AppendLine(" information_schema.columns")
        Query.SQL.AppendLine("where")
        Query.SQL.AppendLine("table_schema=?table_schema")
        Query.SQL.AppendLine("and table_name=?table_name")
        Query.SQL.AppendLine("order by")
        Query.SQL.AppendLine(" ORDINAL_POSITION")
        Query.DeclareAndSet("table_schema", _Owner.TableSchema)
        Query.DeclareAndSet("table_name", _Owner.TableName)
        Query.Execute()
        While Not Query.Eof
            Dim item As New TMyTableColumn
            item._TableCatalog = Query.FieldAsString("TABLE_CATALOG")
            item._TableSchema = Query.FieldAsString("TABLE_SCHEMA")
            item._TableName = Query.FieldAsString("TABLE_NAME")
            item._ColumnName = Query.FieldAsString("COLUMN_NAME")
            item._OrdinalPosition = Query.FieldAsInteger("ORDINAL_POSITION")
            item._ColumnDefault = Query.FieldAsString("COLUMN_DEFAULT")
            item._IsNullable = Query.FieldAsString("IS_NULLABLE")
            item._DataType = Query.FieldAsString("DATA_TYPE")
            item._CharacterMaximumLength = Query.FieldAsInteger("CHARACTER_MAXIMUM_LENGTH")
            item._CharacterOctetLength = Query.FieldAsInteger("CHARACTER_OCTET_LENGTH")
            item._NumericPrecision = Query.FieldAsInteger("NUMERIC_PRECISION")
            item._NumericScale = Query.FieldAsInteger("NUMERIC_SCALE")
            item._CharacterSetName = Query.FieldAsString("CHARACTER_SET_NAME")
            item._CollationName = Query.FieldAsString("COLLATION_NAME")
            item._ColumnType = Query.FieldAsString("COLUMN_TYPE")
            item._ColumnKey = Query.FieldAsString("COLUMN_KEY")
            item._Extra = Query.FieldAsString("EXTRA")
            item._Privileges = Query.FieldAsString("PRIVILEGES")
            item._ColumnComment = Query.FieldAsString("COLUMN_COMMENT")
            _List.Add(item)
            Query.MoveNext()
        End While
        Query.Close()
    End Sub

    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property

    Default Public ReadOnly Property Items(ByVal Index As Integer) As TMyTableColumn
        Get
            Return _List(Index)
        End Get
    End Property
End Class
